package com.cee.util;


import com.cee.common.result.helpers.JwtHelper;

import javax.servlet.http.HttpServletRequest;

//获取当前用户信息工具类
public class AuthContextHolder {
    // 放在当前线程中
    private static  final ThreadLocal<Long> threadLocal = new ThreadLocal<>();
    //获取当前用户id
    public static Long getUserId(HttpServletRequest request) {
        //从header获取token
        String token = request.getHeader("token");
        //jwt从token获取userid
        Long userId = JwtHelper.getUserId(token);
        return userId;
    }

    public static void setUserId(Long userId) {
        threadLocal.set(userId);
    }
    public static Long getUserId(){
        return threadLocal.get();
    }

    //获取当前用户名称
    public static String getUserName(HttpServletRequest request) {
        //从header获取token
        String token = request.getHeader("token");
        //jwt从token获取userid
        String userName = JwtHelper.getUserName(token);
        return userName;
    }
}
